﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Collections;

namespace DAO
{
    public class TaiKhoanDAO
    {
        /// <summary>
        /// Lay thong tin tai khoan
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>null - if wrong password, username or not exist</returns>
        public TaiKhoanDTO layThongTinTaiKhoan(string username, string password)
        {
            List<String> paraArr = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraArr.Add("@username");
            paraArr.Add("@password");
            valueArr.Add(username);
            valueArr.Add(password);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_TaiKhoan_layThongTinTaiKhoanTheoTaiKhoanDN", paraArr, valueArr);
                if (dt.Rows.Count == 0)
                    return null;
                TaiKhoanDTO tk = new TaiKhoanDTO();
                tk.MaTaiKhoan = dt.Rows[0]["MaTaiKhoan"].ToString();
                tk.Username = dt.Rows[0]["Username"].ToString();
                tk.Password = dt.Rows[0]["Password"].ToString();
                tk.TrangThai = int.Parse(dt.Rows[0]["TrangThai"].ToString());
                return tk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Lay thong tin tai khoan
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>null - if wrong password, username or not exist</returns>
        /// <proc>sp_TaiKhoan_layThongTinTaiKhoanTheoTenDN</proc>
        public TaiKhoanDTO layThongTinTaiKhoan(string username)
        {
            List<String> paraArr = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraArr.Add("@username");
            valueArr.Add(username);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_TaiKhoan_layThongTinTaiKhoanTheoTenDN", paraArr, valueArr);
                if (dt.Rows.Count == 0)
                    return null;
                TaiKhoanDTO tk = new TaiKhoanDTO();
                tk.MaTaiKhoan = dt.Rows[0]["MaTaiKhoan"].ToString();
                tk.Username = dt.Rows[0]["Username"].ToString();
                tk.Password = dt.Rows[0]["Password"].ToString();
                tk.TrangThai = int.Parse(dt.Rows[0]["TrangThai"].ToString());
                return tk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Them tai khoan moi vao he thong (ma tai khoan tu dong phat sinh
        /// </summary>
        /// <param name="tk">Tai khoan</param>
        /// <returns>string - ma tai khoan moi them vao</returns>
        /// <proc>sp_TaiKhoan_themTaiKhoan</proc>
        public string themTaiKhoan(TaiKhoanDTO tk)
        {
            List<string> paraLst = new List<string>();
            ArrayList valArr = new ArrayList();
            paraLst.Add("@username");
            paraLst.Add("@password");
            paraLst.Add("@status");
            valArr.Add(tk.Username);
            valArr.Add(tk.Password);
            valArr.Add(tk.TrangThai);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_TaiKhoan_themTaiKhoan", paraLst, valArr);
                if (dt.Rows.Count == 0)
                    return null;
                return dt.Rows[0]["MaTaiKhoan"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// Dang ky tai khoan moi - input dau vao la thong tin - khong can ma tai khoan hay ma khach hang
        /// </summary>
        /// <param name="tk">Tai khoan</param>
        /// <param name="kh">Khach hang</param>
        /// <returns></returns>
        public string dangKyTaiKhoanMoi(TaiKhoanDTO tk, KhachHangDTO kh)
        {
            List<string> paraLst = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraLst.Add("@username");
            paraLst.Add("@password");
            paraLst.Add("@status");
            paraLst.Add("@hoten");
            paraLst.Add("@cmnd");
            paraLst.Add("@diachi");
            paraLst.Add("@email");
            paraLst.Add("@sdt");
            paraLst.Add("@msthe");
            paraLst.Add("@loaithe");
            valueArr.Add(tk.Username);
            valueArr.Add(tk.Password);
            valueArr.Add(tk.TrangThai);
            valueArr.Add(kh.HoTen);
            valueArr.Add(kh.CMND);
            valueArr.Add(kh.DiaChi);
            valueArr.Add(kh.Email);
            valueArr.Add(kh.SDT);
            valueArr.Add(kh.MaSoThe);
            valueArr.Add(kh.LoaiThe);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_TaiKhoan_KhachHang_dangKyTaiKhoanMoi", paraLst, valueArr);
                if (dt.Rows.Count == 0)
                    return null;
                return dt.Rows[0]["MaKhachHang"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// Kich hoat tai khoan - thay doi trang thai tai khoan sang 1
        /// </summary>
        /// <param name="username"></param>
        /// <returns>true - neu kich hoat duoc tai khoan</returns>
        /// <proc>sp_TaiKhoan_kichHoatTaiKhoan</proc>
        public bool kichHoatTaiKhoan(string username)
        {
            List<string> paraLst = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraLst.Add("@username");
            valueArr.Add(username);
            try
            {
                DataProvider.ExecuteStoreProc("sp_TaiKhoan_kichHoatTaiKhoan", paraLst, valueArr);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
